package com.ty.controller;

import com.github.pagehelper.PageInfo;
import com.ty.pojo.Team;
import com.ty.service.TeamService;
import com.ty.vo.QueryVO;
import com.ty.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 球队的控制器层
 *
 * @author 田野
 */
@Controller
@RequestMapping("/team")
@ResponseBody  // 该类下的所有返回内容都是字符串格式的
public class TeamController {

    @Resource
    private TeamService teamService;

    // 如果在QueryVO类中没有在日期上添加注解，可以在这里进行统一的设置
    // 如果实体类中的日期类型需要从页面中获取数据，避免NULL转化成Date类型出问题
    // 解决方案一：@InitBinder
    // 解决方案二：实体类的日期类型属性上添加注解 @DateTimeFormat(pattern = "yyyy-MM-dd")
    /*@InitBinder
    protected  void initDateFormatBinder(WebDataBinder binder){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }*/


    // 多条件查询
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultVO<Team> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo) {

        System.out.println(vo);

        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }

        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }

        PageInfo<Team> teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(teamPageInfo);
    }

    // 添加一个球队
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Team> addTeam(Team team) {

        System.out.println(team);

        int i = teamService.addTeam(team);
        if (i == 1) {
            return new ResultVO<>();
        }
        return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
    }

    // 根据主键查询
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Team> queryById(@PathVariable("id") Integer teamId) {
        Team team = teamService.queryById(teamId);
        ResultVO<Team> vo = new ResultVO<>(team);
        return vo;
    }

    // 根据主键更新
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Team> updateTeam(@PathVariable("id") Integer teamId, Team team) {
        // 由于前端的这个 teamId ，是隐藏的，所以传不到这里来，需要手动的设置
        team.setTeamId(teamId);
        int i = teamService.updateTeam(team);
        if (i == 1) {
            return new ResultVO<>();
        } else {
            return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
        }
    }

    // 根据主键删除
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Team> deleteTeam(@PathVariable("id") Integer teamId) {
        int i = teamService.deleteTeam(teamId);
        if (i == 1) {
            return new ResultVO<>();
        } else {
            return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
        }

    }

    // 文件上传
    /**
     * @return
     */
    @RequestMapping("{id}")
    public ResultVO<Team> uploadLogo(@RequestParam("logo") MultipartFile myFile,
                                     HttpServletRequest request,
                                     @PathVariable("id") Integer teamId) {
        // 文件上传到指定的服务器位置
        String path = request.getServletContext().getRealPath("/img/uploadFile");
        // 获取原始文件的名称
        String originalFilename = myFile.getOriginalFilename();
        // 定义文件的新名称：随机生成的名称+原有文件的后缀名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String logoName = randomName + suffix;
        int num = 0;
        try {
            myFile.transferTo(new File(path + "/" + logoName));
            System.out.println("上传成功！" + path + "/" + logoName);
            // 将图片的名称更新到数据库中
            Team team = new Team();
            team.setTeamId(teamId);
            // 动态更新，只更新这一列
            team.setTeamLogo(logoName);
            num = teamService.updateTeam(team);
            if (num == 1) {
                return new ResultVO<>();
            } else {
                return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO<>(500, "图片上传出现问题！" + e.getMessage());
        }

    }


}
